package com.example.wecomservice.controller.callback;

import com.example.wecomservice.pojo.dto.OauthDto;
import com.example.wecomservice.service.IWeComService;
import com.example.wecomservice.utils.SignatureUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jamin
 * @Date 2023/2/3 10:58
 */
@Api(tags = "企业微信回调")
@RestController
@RequestMapping("/callback/weCom")
public class WeComCallbackController {

    @Autowired
    IWeComService weComService;

    @Value("${domain}")
    private String domain;

    @ApiOperation("企业微信服务器配置地址，用于验证启用 应用管理-回调配置-数据回调URL")
    @GetMapping(value = "/msg")
    @ResponseBody
    public String weComMsgVerify(HttpServletRequest request) {
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        String echostr = request.getParameter("echostr");// 随机字符串

        // 确认此次GET请求来自企业微信服务器，原样返回echostr参数内容
        return weComService.weComVerify(signature, timestamp, nonce, echostr);
    }

    @ApiOperation("企业微信服务器配置地址，用于验证启用 应用管理-回调配置-指令回调URL")
    @GetMapping(value = "/command")
    @ResponseBody
    public String weComCommandVerify(HttpServletRequest request) {
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        String echostr = request.getParameter("echostr");// 随机字符串

        // 确认此次GET请求来自企业微信服务器，原样返回echostr参数内容
        return weComService.weComVerify(signature, timestamp, nonce, echostr);
    }

    @ApiOperation("企业微信，指令回调（刷新ticket）")
    @PostMapping(value = "/command")
    @ResponseBody
    public String weComCommand(HttpServletRequest request) throws Exception {
        String signature = request.getParameter("msg_signature");// 微信加密签名
        String timestamp = request.getParameter("timestamp");// 时间戳
        String nonce = request.getParameter("nonce");// 随机数
        ServletInputStream in = request.getInputStream();
        String success = weComService.weComCommand(signature, timestamp, nonce, in);
        return success;
    }

    @ApiOperation("企业微信授权")
    @GetMapping("/oauth")
    @ResponseBody
    public void oauth(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        String state = request.getParameter("state"); //state是自己添加的参数，可以根据场景自定义
        OauthDto oauthDto = weComService.oauth(code, state);
        String authCorpId = oauthDto.getAuthCorpId();
        String openUserId = oauthDto.getOpenUserId();
        String timestamp = System.currentTimeMillis() + "";
        String signature = SignatureUtil.getSignature(authCorpId + "_" + openUserId + "_" + timestamp, timestamp);
        //实际登录地址，或者根据场景做跳转
        String loginUrl;
        if (state.equals("weCom-web")){
            loginUrl = domain + "/wecom/web/weCom/autoLogin?authCorpId=";
        }else {
            loginUrl = domain + "/wecom/app/weCom/autoLogin?authCorpId=";
        }
        response.sendRedirect(loginUrl + authCorpId + "&openUserId=" + openUserId + "&timestamp=" + timestamp + "&signature=" + signature);
    }
}
